/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.ufba.lasid.jitclouds.mda.datatypes;

import br.ufba.lasid.jitclouds.mda.api.IVirtualMachineHandler;
import java.io.BufferedReader;
import java.io.InputStreamReader;

/**
 * XenVMHandler is a handler for Xen Virtual Machine instances
 * @author aliriosa
 */
public class XenVMHandler implements IVirtualMachineHandler {

    String vmid = null;
    
    @Override
    public String getStatus() throws Exception {
        System.out.println("[DEBUG]" + "xm domstate " + vmid);
        Process p = Runtime.getRuntime().exec("xm domstate " + vmid);
        BufferedReader in = new BufferedReader(
            new InputStreamReader(p.getInputStream())
        );
        
        String pout = in.readLine();
        
        if(pout != null){
            
            pout = pout.toLowerCase();
            
            if(pout.indexOf("error") >=0){
                pout = "error";
            }else if(pout.indexOf("paused") >=0){
                pout = "paused";
            }
        }
        return pout;
    }

    public static String XEN_CHK_HOME = "/mnt/drbd1";
    
    public static String XEN_CFG_HOME = "/etc/xen";
        protected String getConfigFile(){
        return XEN_CFG_HOME + "/" + vmid + ".cfg";
    }
    
    public void setID(String id){
        vmid = id;
    }
    
    @Override
    public String getID(){
        return vmid;
    }
    
    @Override
    public void create() throws Exception {
        System.out.println("[DEBUG]" + "xm create -f " + XEN_CFG_HOME + "/" + vmid + ".cfg");
        Runtime.getRuntime().exec("xm create -f " + XEN_CFG_HOME + "/" + vmid + ".cfg");
        
    }

    @Override
    public void pause() throws Exception {
        System.out.println("[DEBUG]" + "xm pause " + vmid);
        Runtime.getRuntime().exec("xm pause " + vmid);
    }

    @Override
    public void checkpoint() throws Exception {
        System.out.println("[DEBUG]" + "xm save -c -f " + vmid + " " + XEN_CHK_HOME + "/" +vmid + ".checkpoint");
        Runtime.getRuntime().exec("xm save -c -f " + vmid + " " + XEN_CHK_HOME + "/" + vmid + ".checkpoint");
    }

    @Override
    public void restore() throws Exception {
        System.out.println("[DEBUG]" + "xm restore " + XEN_CHK_HOME + "/" + vmid + ".checkpoint -p");
        Runtime.getRuntime().exec("xm restore " + XEN_CHK_HOME + "/" + vmid + ".checkpoint -p");
    }

    @Override
    public void connect(String vmid) throws Exception {
        setID(vmid);
    }

    @Override
    public void destroy() throws Exception {
        System.out.println("[DEBUG]" + "xm destroy " + vmid);
        Runtime.getRuntime().exec("xm destroy " + vmid);
    }

    @Override
    public void unpause() throws Exception {
        System.out.println("[DEBUG]" + "xm unpause " + vmid);
        Runtime.getRuntime().exec("xm unpause " + vmid);
    }

    @Override
    public void restoreAndRun() throws Exception {
        System.out.println("[DEBUG]" + "xm restore " + vmid + ".checkpoint");
        Runtime.getRuntime().exec("xm restore " + vmid + ".checkpoint");
    }

    @Override
    public String toString() {
        return vmid;
    }

    
    
}
